set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_Article_GetInfo]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@n4ArticleSN	int	
,	@n1BoardSN	tinyint	= NULL	OUTPUT
,	@n4CategoryNo	int = null output
,	@strTitle	nvarchar(400)	= NULL	OUTPUT
,	@strContent	nvarchar(MAX)	= NULL	OUTPUT
,	@strJSONData	nvarchar(MAX)		= null	OUTPUT
,	@strFileUrl	varchar(1000)	= null	OUTPUT
,	@n4ManagerSNWriter	int	= NULL	OUTPUT
,	@strUserID			varchar(50)	= null	OUTPUT
,	@dtCreatedDate	datetime	= NULL	OUTPUT
,	@dtModifiedDate	datetime	= NULL	OUTPUT
,	@n4ReadCount	int	= NULL	OUTPUT

,	@isRequireAdjacentInfo		bit = 0
,	@strTitle_Search			nvarchar(400)	= null
,	@isUpdateReadCount		bit = 1

,	@n4ArticleSN_next			int				= null	OUTPUT
,	@n4ArticleSN_prev			int				= null	OUTPUT

,	@strTitle_next				nvarchar(400)	= null	OUTPUT
,	@strTitle_prev				nvarchar(400)	= null	OUTPUT
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

-- Implementation

IF ( @IsUpdateReadCount = 1 )
BEGIN
	Update
		dbo.TB_Article
	Set
		@n1BoardSN = n1BoardSN
	,	@n4CategoryNo = n4CategoryNo
	,	@strTitle = strTitle
	,	@strContent = strContent
	,	@strJSONData = strJSONData
	,	@strFileUrl = strFileUrl
	,	@n4ManagerSNWriter = n4ManagerSNWriter
	,	@strUserID = strUserID
	,	@dtCreatedDate = dtCreatedDate
	,	@dtModifiedDate = dtModifiedDate
	,	@n4ReadCount = n4ReadCount = n4ReadCount + 1	
	Where
		@n4ArticleSN = n4ArticleSN
END ELSE BEGIN
	SELECT
		@n1BoardSN = n1BoardSN
	,	@n4CategoryNo = n4CategoryNo
	,	@strTitle = strTitle
	,	@strContent = strContent
	,	@strJSONData = strJSONData
	,	@strFileUrl = strFileUrl
	,	@n4ManagerSNWriter = n4ManagerSNWriter
	,	@strUserID = strUserID
	,	@dtCreatedDate = dtCreatedDate
	,	@dtModifiedDate = dtModifiedDate
	,	@n4ReadCount = n4ReadCount
	FROM
		dbo.TB_Article with ( nolock )
	Where
		@n4ArticleSN = n4ArticleSN
END

if @isRequireAdjacentInfo = 1
begin
	Select top(1)
		@n4ArticleSN_next = n4ArticleSN,
		@strTitle_next = strTitle
	From
		dbo.TB_Article with (nolock)
	Where
		n1BoardSN = @n1BoardSN
	and	n4ArticleSN > @n4ArticleSN
	and strTitle like ('%' + isnull(@strTitle_search, '') + '%')
	order by
		n1BoardSN asc
	,	n4ArticleSN asc

	Select top(1)
		@n4ArticleSN_prev = n4ArticleSN,
		@strTitle_prev = strTitle
	From
		dbo.TB_Article with (nolock)
	Where
		n1BoardSN = @n1BoardSN
	and	n4ArticleSN < @n4ArticleSN
	and strTitle like ('%' + isnull(@strTitle_search, '') + '%')
	order by
		n1BoardSN desc
	,	n4ArticleSN desc
end

-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
